package com.open.capacity.workflow3.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow3.dao.NwfSchemeAuthDao;
import com.open.capacity.workflow3.dao.NwfSchemeinfoDao;
import com.open.capacity.workflow3.entity.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.workflow3.dao.NwfSchemeDao;
import com.open.capacity.workflow3.service.NwfSchemeService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class NwfSchemeServiceImpl implements NwfSchemeService {

    @Autowired
    private NwfSchemeDao schemeDao;

    @Autowired
    private NwfSchemeinfoDao schemeinfoDao;

    @Autowired
    private NwfSchemeAuthDao schemeAuthDao;

    public PageResult<NwfSchemeinfo> getInfoPageList(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null) PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<NwfSchemeinfo> list = schemeinfoDao.findAll(params);
        PageInfo<NwfSchemeinfo> pageInfo = new PageInfo(list);

        return PageResult.<NwfSchemeinfo>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    public List<NwfSchemeinfo> getInfoList(SysUser userInfo) {
        String userId = userInfo.getId();
        String postIds = userInfo.getPostIds();
        String roleIds = userInfo.getRoleIds();
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        if (StringUtil.isNotEmpty(postIds)) {
            param.put("postIds", postIds.split(","));
        }
        if (StringUtil.isNotEmpty(roleIds)) {
            param.put("roleIds", roleIds.split(","));
        }

        List<NwfSchemeAuth> authList = schemeAuthDao.findAll(param);

        param = new HashMap<>();
        param.put("enabledMark", 1);
        param.put("mark", 1);
        List<String> ids = authList.stream().map(NwfSchemeAuth::getSchemeinfoId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(ids)) {
            param.put("ids", ids);
        }

        List<NwfSchemeinfo> list = schemeinfoDao.findAll(param);
        return list;
    }

    public List<NwfSchemeinfo> getInfoList() {
        Map<String, Object> param = new HashMap<>();
        param.put("enabledMark", 1);

        List<NwfSchemeinfo> list = schemeinfoDao.findAll(param);
        return list;
    }


    public NwfSchemeinfo getInfoEntity(String keyValue) {
        return schemeinfoDao.findById(keyValue);
    }

    public NwfSchemeinfo getInfoEntityByCode(String code) {
        return schemeinfoDao.findByCode(code);
    }

    public List<NwfSchemeAuth> getAuthList(String schemeInfoId) {
        List<NwfSchemeAuth> list = schemeAuthDao.findBySchemeInfoId(schemeInfoId);
        return list;
    }

    public PageResult<NwfScheme> getSchemePageList(String schemeInfoId, Map<String, Object> params) {

        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null) PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<NwfScheme> list = schemeDao.findBySchemeinfoId(schemeInfoId);
        PageInfo<NwfScheme> pageInfo = new PageInfo(list);

        return PageResult.<NwfScheme>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    public NwfScheme getSchemeEntity(String keyValue) {
        return schemeDao.findById(keyValue);
    }

    @Transactional
    public void delete(String keyValue) {
        schemeinfoDao.delete(keyValue);
        schemeDao.deleteBySchemeinfoId(keyValue);
        schemeAuthDao.deleteBySchemeinfoId(keyValue);
    }

    @Transactional
    public void save(String keyValue, NwfSchemeinfo infoEntity, NwfScheme schemeEntity, List<NwfSchemeAuth> authList) {
        if (StringUtil.isNotEmpty(keyValue)) {
            NwfScheme oldNwfScheme = schemeDao.findById(infoEntity.getSchemeId());
            if (oldNwfScheme.getContent().equals(schemeEntity.getContent()) && oldNwfScheme.getType().equals(schemeEntity.getType())) {
                schemeEntity = null;
            }
        }
        SysUser currentUser = SysUserUtil.getLoginAppUser();
        if (StringUtil.isEmpty(keyValue)) {
            infoEntity = infoEntity.create();
        } else {
            infoEntity = infoEntity.modify(keyValue);
        }
        if (schemeEntity != null) {
            schemeEntity.setSchemeinfoId(infoEntity.getId());
            schemeEntity = schemeEntity.create();
            schemeEntity.setCreateTime(new Date());
            schemeEntity.setCreateUserid(currentUser.getId());
            schemeEntity.setCreateUsername(currentUser.getRealName());

            schemeDao.save(schemeEntity);
            infoEntity.setSchemeId(schemeEntity.getId());
        }
        if (StringUtil.isNotEmpty(keyValue)) {
            schemeinfoDao.update(infoEntity);
        } else {
            schemeinfoDao.save(infoEntity);
        }
        String schemeInfoId = infoEntity.getId();
        schemeAuthDao.deleteBySchemeinfoId(schemeInfoId);
        for (NwfSchemeAuth item : authList) {
            item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            item.setSchemeinfoId(schemeInfoId);
            schemeAuthDao.save(item);
        }
    }

    public void updateScheme(String schemeInfoId, String schemeId) {
        NwfScheme schemeEntity = schemeDao.findById(schemeId);
        NwfSchemeinfo entity = new NwfSchemeinfo();
        entity.setId(schemeInfoId);
        entity.setSchemeId(schemeId);

        if (schemeEntity.getType() != 1) {
            entity.setEnabledMark(0);
        }
        schemeinfoDao.update(entity);
    }

    public void updateState(String schemeInfoId, int state) {
        NwfSchemeinfo entity = new NwfSchemeinfo();
        entity.setId(schemeInfoId);
        entity.setEnabledMark(state);
        schemeinfoDao.update(entity);
    }
}
